namespace TSL.DAL
{
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using System.Text;
    using TSL.Model;

    public class SSC_EndTime
    {
        public void AddSSC_EndTime(TSL.Model.SSC_EndTime model)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("TSL_SSC_EndTime_ADD");
            db.AddInParameter(dbCommand, "lottery_num", DbType.AnsiString, model.lottery_num);
            db.AddInParameter(dbCommand, "begintime", DbType.AnsiString, model.begintime);
            db.AddInParameter(dbCommand, "endtime1", DbType.AnsiString, model.endtime1);
            db.AddInParameter(dbCommand, "endtime2", DbType.AnsiString, model.endtime2);
            db.AddInParameter(dbCommand, "resttime", DbType.AnsiString, model.resttime);
            db.AddInParameter(dbCommand, "restday", DbType.AnsiString, model.restday);
            db.AddInParameter(dbCommand, "state", DbType.AnsiString, model.state);
            db.AddInParameter(dbCommand, "issale", DbType.AnsiString, model.issale);
            db.AddInParameter(dbCommand, "ssc_id", DbType.AnsiString, model.ssc_id);
            db.ExecuteNonQuery(dbCommand);
        }

        public void Delete(string ID)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("TSL_SSC_EndTime_Delete");
            db.AddInParameter(dbCommand, "ID", DbType.AnsiString, ID);
            db.ExecuteNonQuery(dbCommand);
        }

        public void DeleteSSC_EndTime(string ID)
        {
            List<string> strList = new List<string>();
            strList.Add("delete from SSC_EndTime where ID = '" + ID + "'");
            DbHelperSQL.ExecuteSqlTran(strList);
        }

        public bool Exists(string name)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from SSC_EndTime");
            strSql.Append(" where lottery_num=@name ");
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@name", SqlDbType.VarChar, 50) };
            parameters[0].Value = name;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        public bool Exists(string ssc_id, string lottery_num)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from SSC_EndTime");
            strSql.Append(" where lottery_num=@lottery_num and ssc_id=@ssc_id");
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ssc_id", SqlDbType.VarChar, 50), new SqlParameter("@lottery_num", SqlDbType.VarChar, 50) };
            parameters[0].Value = ssc_id;
            parameters[1].Value = lottery_num;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        public List<TSL.Model.SSC_EndTime> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from V_SSC_EndTime ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append("order by id asc");
            List<TSL.Model.SSC_EndTime> list = new List<TSL.Model.SSC_EndTime>();
            using (IDataReader dataReader = DatabaseFactory.CreateDatabase().ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(this.ReaderBind(dataReader));
                }
            }
            return list;
        }

        public DataSet GetListFromView(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from V_SSC_EndTime order by id asc");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DatabaseFactory.CreateDatabase().ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        public TSL.Model.SSC_EndTime GetModel(string ID)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("SYS_SSC_EndTime_GetModelByID");
            db.AddInParameter(dbCommand, "ID", DbType.AnsiString, ID);
            TSL.Model.SSC_EndTime model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = this.ReaderBind(dataReader);
                }
            }
            return model;
        }

        public TSL.Model.SSC_EndTime GetModel(string name, string password)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("SYS_SSC_EndTime_GetModel");
            db.AddInParameter(dbCommand, "username", DbType.AnsiString, name);
            db.AddInParameter(dbCommand, "password", DbType.AnsiString, password);
            TSL.Model.SSC_EndTime model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = this.ReaderBind(dataReader);
                }
            }
            return model;
        }

        public TSL.Model.SSC_EndTime ReaderBind(IDataReader dataReader)
        {
            TSL.Model.SSC_EndTime model = new TSL.Model.SSC_EndTime();
            model.id = dataReader["id"].ToString();
            model.lottery_num = dataReader["lottery_num"].ToString();
            model.begintime = dataReader["begintime"].ToString();
            model.endtime1 = dataReader["endtime1"].ToString();
            model.endtime2 = dataReader["endtime2"].ToString();
            model.resttime = dataReader["resttime"].ToString();
            model.restday = dataReader["restday"].ToString();
            model.state = dataReader["state"].ToString();
            model.issale = dataReader["issale"].ToString();
            model.ssc_id = dataReader["ssc_id"].ToString();
            model.sscname = dataReader["sscname"].ToString();
            return model;
        }

        public void Update(TSL.Model.SSC_EndTime model)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("SYS_SSC_EndTime_Update");
            db.AddInParameter(dbCommand, "id", DbType.AnsiString, model.id);
            db.AddInParameter(dbCommand, "lottery_num", DbType.AnsiString, model.lottery_num);
            db.AddInParameter(dbCommand, "begintime", DbType.AnsiString, model.begintime);
            db.AddInParameter(dbCommand, "endtime1", DbType.AnsiString, model.endtime1);
            db.AddInParameter(dbCommand, "endtime2", DbType.AnsiString, model.endtime2);
            db.AddInParameter(dbCommand, "resttime", DbType.AnsiString, model.resttime);
            db.AddInParameter(dbCommand, "restday", DbType.AnsiString, model.restday);
            db.AddInParameter(dbCommand, "state", DbType.AnsiString, model.state);
            db.AddInParameter(dbCommand, "issale", DbType.AnsiString, model.issale);
            db.AddInParameter(dbCommand, "ssc_id", DbType.AnsiString, model.ssc_id);
            db.ExecuteNonQuery(dbCommand);
        }
    }
}

